<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name" required>
        <el-input v-model="form.name" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="角色码" prop="code" required>
        <el-input v-model="form.code" placeholder="请输入角色码" />
      </el-form-item>
      <!--<el-form-item label="数据权限" prop="dataScope">-->
      <!--  <el-input v-model="form.dataScope" placeholder="请输入数据权限" />-->
      <!--</el-form-item>-->
      <el-form-item label="用户状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1" size="large">启用</el-radio>
          <el-radio :label="0" size="large">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <!--      <el-form-item label="按钮权限">-->
      <!--        <el-checkbox-group v-model="form.btnRolesCheckedList">-->
      <!--          <el-checkbox-->
      <!--            v-for="item in btnRoles"-->
      <!--            :key="item.value"-->
      <!--            :label="item.value"-->
      <!--          >-->
      <!--            {{ item.lable }}-->
      <!--          </el-checkbox>-->
      <!--        </el-checkbox-group>-->
      <!--      </el-form-item>-->
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/roleManagement'

  export default defineComponent({
    name: 'RoleManagementEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        MenusList: [],
        ids: [],
        formRef: null,
        treeRef: null,
        form: {
          btnRolesCheckedList: [],
        },
        rules: {
          name: [
            { required: true, trigger: 'blur', message: '请输入角色名称' },
          ],
          code: [{ required: true, trigger: 'blur', message: '请输入角色码' }],
          //          dataScope: [
          //            { required: true, trigger: 'blur', message: '请输入数据权限' },
          //          ],
        },
        title: '',
        dialogFormVisible: false,
        list: [],
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = JSON.parse(JSON.stringify(row))
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          btnRolesCheckedList: [],
        }
        state.ids = []
        state.MenusList = []
        state.dialogFormVisible = false
      }
      // const fetchData = async () => {
      //   const { data: data } = await getList()
      //   state.list = data
      // }

      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            try {
              const { msg } = await doEdit({
                ...state.form,
              })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              emit('fetch-data')
              close()
            } catch (e) {
              console.log(e)
            }
          }
        })
      }
      onMounted(() => {
        // fetchData()
      })
      return {
        ...toRefs(state),
        showEdit,
        close,
        // fetchData,
        save,
      }
    },
  })
</script>

<style lang="scss" scoped>
  .vab-tree-border {
    width: 100%;
    height: 250px;
    padding: $base-padding;
    overflow-y: auto;
    border: 1px solid #dcdfe6;
    border-radius: $base-border-radius;
  }
</style>
